<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>按钮权限</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${ctx}/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="${ctx}/systemStatic/style/admin.css">
    <link rel="stylesheet" href="${ctx}/iconfont/iconfont.css"/>
    <link rel="stylesheet" href="${ctx}/css/public.css">
    <script type="text/javascript">
        var __ctx = "${ctx}";
    </script>
</head>
<style>
    .layui-table td,.layui-table th{text-align: center;}
    .layui-textarea{
        min-height: 60px;
        width: 220px;
    }
    .privateSubmit{
        margin: 0 10px;
    }
    .layui-table th,.layui-table td{
        padding: 5px 0;
    }
</style>
<body>
<form class="layui-form" style="padding: 10px;">
    <button class="layui-btn btn-n privateSubmit" lay-submit lay-filter="privateSubmit">提交</button>
    <table class="layui-table">
        <thead>
            <tr>
                <th style="font-weight: bold;">名称</th>
                <th>
                    <div class="layui-inline">
                        <label class="layui-form-label" style="font-weight: bold;">权限</label>
                        <div class="layui-input-inline">
                            <select name="interest" lay-filter="manageFields">
                                <option value="">请选择</option>
                                <option value="none">无</option>
                                <option value="everyone">所有人</option>
                            </select>
                        </div>
                    </div>
                </th>
            </tr>
        </thead>
        <tbody class="layui-form tableBody" lay-filter="tableBody">
            <tr>
                <td class="slNames" name="addFolder">新建文件夹</td>
                <td>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <select name="permissionType" lay-filter="permissionType">
                                <option value="">请选择</option>
                                <option value="none">无</option>
                                <option value="everyone">所有人</option>
                                <option value="user">用户</option>
                                <option value="role">角色</option>
                                <option value="org">组织</option>
                            </select>
                        </div>
                        <textarea style="display: none;" name="cmpIds" class="hiddenJson"></textarea>
                        <textarea style="display: inline-block;vertical-align: middle;" rows="2" class="textarea layui-textarea" readonly="readonly"></textarea>
                        <a class="windowButton layui-btn layui-btn-sm" ><span>选择...</span></a>
                        <span class="font" style='vertical-align: middle;color: #a9302a;'>必填</span>
                    </div>
                </td>
            </tr>
            <tr>
                <td class="slNames" name="addFile">上传文件</td>
                <td>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <select name="permissionType" lay-filter="permissionType">
                                <option value="">请选择</option>
                                <option value="none">无</option>
                                <option value="everyone">所有人</option>
                                <option value="user">用户</option>
                                <option value="role">角色</option>
                                <option value="org">组织</option>
                            </select>
                        </div>
                        <textarea style="display: none;" name="cmpIds" class="hiddenJson"></textarea>
                        <textarea style="display: inline-block;vertical-align: middle;" rows="2" class="textarea layui-textarea" readonly="readonly"></textarea>
                        <a class="windowButton layui-btn layui-btn-sm"  ><span>选择...</span></a>
                        <span class="font" style='vertical-align: middle;color: #a9302a;'>必填</span>
                    </div>
                </td>
            </tr>
            <tr>
                <td class="slNames" name="download">下载</td>
                <td>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <select name="permissionType" lay-filter="permissionType">
                                <option value="">请选择</option>
                                <option value="none">无</option>
                                <option value="everyone">所有人</option>
                                <option value="user">用户</option>
                                <option value="role">角色</option>
                                <option value="org">组织</option>
                            </select>
                        </div>
                        <textarea style="display: none;" name="cmpIds" class="hiddenJson"></textarea>
                        <textarea style="display: inline-block;vertical-align: middle;" rows="2" class="textarea layui-textarea" readonly="readonly"></textarea>
                        <a class="windowButton layui-btn layui-btn-sm"  ><span>选择...</span></a>
                        <span class="font" style='vertical-align: middle;color: #a9302a;'>必填</span>
                    </div>
                </td>
            </tr>
            <tr>
                <td class="slNames" name="edit">编辑</td>
                <td>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <select name="permissionType" lay-filter="permissionType">
                                <option value="">请选择</option>
                                <option value="none">无</option>
                                <option value="everyone">所有人</option>
                                <option value="user">用户</option>
                                <option value="role">角色</option>
                                <option value="org">组织</option>
                            </select>
                        </div>
                        <textarea style="display: none;" name="cmpIds" class="hiddenJson"></textarea>
                        <textarea style="display: inline-block;vertical-align: middle;" rows="2" class="textarea layui-textarea" readonly="readonly"></textarea>
                        <a class="windowButton layui-btn layui-btn-sm"  ><span>选择...</span></a>
                        <span class="font" style='vertical-align: middle;color: #a9302a;'>必填</span>
                    </div>
                </td>
            </tr>
            <tr>
                <td class="slNames" name="addMapping">文件映射</td>
                <td>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <select name="permissionType" lay-filter="permissionType">
                                <option value="">请选择</option>
                                <option value="none">无</option>
                                <option value="everyone">所有人</option>
                                <option value="user">用户</option>
                                <option value="role">角色</option>
                                <option value="org">组织</option>
                            </select>
                        </div>
                        <textarea style="display: none;" name="cmpIds" class="hiddenJson"></textarea>
                        <textarea style="display: inline-block;vertical-align: middle;" rows="2" class="textarea layui-textarea" readonly="readonly"></textarea>
                        <a class="windowButton layui-btn layui-btn-sm"  ><span>选择...</span></a>
                        <span class="font" style='vertical-align: middle;color: #a9302a;'>必填</span>
                    </div>
                </td>
            </tr>
            <tr>
                <td class="slNames" name="cancelMapping">取消映射</td>
                <td>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <select name="permissionType" lay-filter="permissionType">
                                <option value="">请选择</option>
                                <option value="none">无</option>
                                <option value="everyone">所有人</option>
                                <option value="user">用户</option>
                                <option value="role">角色</option>
                                <option value="org">组织</option>
                            </select>
                        </div>
                        <textarea style="display: none;" name="cmpIds" class="hiddenJson"></textarea>
                        <textarea style="display: inline-block;vertical-align: middle;" rows="2" class="textarea layui-textarea" readonly="readonly"></textarea>
                        <a class="windowButton layui-btn layui-btn-sm"  ><span>选择...</span></a>
                        <span class="font" style='vertical-align: middle;color: #a9302a;'>必填</span>
                    </div>
                </td>
            </tr>
            <tr>
                <td class="slNames" name="addToResource">添加为菜单</td>
                <td>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <select name="permissionType" lay-filter="permissionType">
                                <option value="">请选择</option>
                                <option value="none">无</option>
                                <option value="everyone">所有人</option>
                                <option value="user">用户</option>
                                <option value="role">角色</option>
                                <option value="org">组织</option>
                            </select>
                        </div>
                        <textarea style="display: none;" name="cmpIds" class="hiddenJson"></textarea>
                        <textarea style="display: inline-block;vertical-align: middle;" rows="2" class="textarea layui-textarea" readonly="readonly"></textarea>
                        <a class="windowButton layui-btn layui-btn-sm"  ><span>选择...</span></a>
                        <span class="font" style='vertical-align: middle;color: #a9302a;'>必填</span>
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
</form>
<script src="${ctx}/extends/jquery.min.js" type="text/javascript"></script>
<script src="${ctx}/public/component.js" type="text/javascript"></script>
<script src="${ctx}/layui/layui.js"></script>
<script>
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','common','laydate','jquery','commons','form','layer','setter'], function () {
        var laydate = layui.laydate;
        var $ = layui.jquery;
        var commons = layui.commons;
        var form = layui.form;
        var layer = layui.layer;
        var setter = layui.setter;
        $(".windowButton,.textarea,.font").hide();
        var dateTimeFiledLen = $(".dateTimeFiled");
        for(var i=0;i<dateTimeFiledLen.length;i++){
            laydate.render({
                elem: dateTimeFiledLen[i] //指定元素
                ,type: 'date'
                ,trigger: 'click'
            });
        }
        //权限信息回显
        var manageField = '${manageField}';
        if (manageField == "") { //如果没有设置过按钮权限，默认是所有人都不能看到
            $("select[name='permissionType']").find("option[value='none']").attr("selected", true);
        } else {
            var arr = JSON.parse(manageField);
            for (i = 0; i < arr.length; i++) {
                var showNames = arr[i].right[0].names;
                var showIds = arr[i].right[0].ids;
                var typeVal = arr[i].right[0].type;
                $('.tableBody tr').eq(i).find("select[name='permissionType']").val(typeVal);
                if (typeVal !=='none'&&typeVal !== 'everyone'){
                    $('.tableBody tr').eq(i).find(".windowButton").show();
                    $('.tableBody tr').eq(i).find(".textarea").show().text(showNames);
                    $('.tableBody tr').eq(i).find(".hiddenJson").text(showIds);
                }
            }
            form.render();
        }
        form.on('submit(privateSubmit)', function(data){
            var flag = true;
            var dataAll = [];
            $.each($('.tableBody tr'),function (k,v) {
                if ($('.tableBody tr').eq(k).find("select[name='permissionType']").val()==''){
                    $('.tableBody tr').eq(k).find(".font").show();
                    flag = false;
                }else {
                    var data = {
                        desc:$('.tableBody tr').eq(k).children(".slNames").text(),
                        name:$('.tableBody tr').eq(k).children(".slNames").attr('name'),
                        right:[]
                    };
                    var rigData = {};
                    if ($('.tableBody tr').eq(k).find("select[name='permissionType']").val()!=='none'&& $('.tableBody tr').eq(k).find("select[name='permissionType']").val()!=='everyone'){
                        rigData.type = $('.tableBody tr').eq(k).find("select[name='permissionType']").val();
                        if ($('.tableBody tr').eq(k).find(".textarea").text() == "" || $('.tableBody tr').eq(k).find(".hiddenJson").text() == "") {
                            layer.msg('请选择对应维度下的数据！', {icon: 0});
                            flag = false;
                        }
                        rigData.names = $('.tableBody tr').eq(k).find(".textarea").text();
                        rigData.ids = $('.tableBody tr').eq(k).find(".hiddenJson").text();
                    }else {
                        rigData.type = $('.tableBody tr').eq(k).find("select[name='permissionType']").val();
                    }
                    data.right.push(rigData);
                    dataAll.push(data)
                }
            })
            var params = {nodeId: -1, permissionType: 1, manageField: JSON.stringify(dataAll)};
            if (flag){
                $.ajax({
                    url: serverPath.systemPath + setter.sysLinkPath + "fileManagementPermission/permissionSave",
                    type: "post",
                    dataType: 'json',
                    data: params,
                    success: function (data) {
                        if (data.code==0){
                            layer.msg('保存成功！', {icon: 6});
                        }else {
                            layer.msg('保存失败！', {icon: 5});
                        }

                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        layer.alert("暂无数据，请稍后操作！" + XMLHttpRequest.status);
                    }
                })
            }

            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        form.on('select(permissionType)', function(data){
            if(data.value!==''){
                $(data.othis).parent().siblings(".font").hide();
                if (data.value!=='none'&&data.value!=='everyone'){
                    $(data.othis).parent().siblings(".windowButton").show();
                    $(data.othis).parent().siblings(".textarea").show().text('');
                }else {
                    $(data.othis).parent().siblings(".windowButton,.textarea").hide();
                    $(data.othis).parent().siblings(".textarea").hide().text('');
                }
            }else {
                $(data.othis).parent().siblings(".windowButton,.textarea").hide();
                $(data.othis).parent().siblings(".textarea").hide().text('');
            }

        });
        form.on('select(manageFields)', function(data){
            $(".windowButton,.textarea,.font").hide();
            $(".textarea").hide().text('');
            $("select[name='permissionType']").val(data.value);
            form.render('select');
        });
        $(document).off('click','.windowButton').on('click', '.windowButton', function () {
            var userSelectorWin;
            var body;
            var itemName='';
            var itemId='';
            var theData = $(this).siblings(".textarea");
            var hiddenData = $(this).siblings(".hiddenJson");
            //获取当前行的选中值
            var val= $(this).parent().find('select[name=permissionType]').val();
            switch (val) {
                case 'user':
                    parent.layer.open({
                        type:2
                        ,title:'人员选择器'
                        ,area:['980px','630px']
                        ,btn:["保存","取消","撤销"]
                        ,resize:false
                        ,content: serverPath.systemPath + setter.sysLinkPath+'dialog/view/sysUserDialog'
                        ,success: function(layero, index){
                            body = parent.layer.getChildFrame('body',index);
                            userSelectorWin = parent.window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                            //向用户选择器页面传递初始值[{name:"name1",userId:"userId1"},{name:"name2",userId:"userId2"}...]
                            if (theData.text()) {
                                userSelectorWin.layui.userList = [];
                                var hiddenDataArr = hiddenData.text().split(",");
                                var theDataArr = theData.text().split(",");
                                $.each(hiddenDataArr,function (k,v) {
                                    var userListObj={};
                                    userListObj.name = theDataArr[k];
                                    userListObj.userId = v;
                                    userSelectorWin.layui.userList.push(userListObj);
                                })
                            }
                        }
                        ,yes:function(index,layero){
                            var theUserList = body.find("input[name=userList]").val();
                            if (theUserList){
                                //用户选择器返回的选择人员数组[{name:"name1",userId:"userId1"},{name:"name2",userId:"userId2"}...]
                                $.each(JSON.parse(theUserList),function (index, item) {
                                    itemName += item.name + ",";
                                    itemId += item.userId + ",";
                                });
                                if (itemName.length > 0) {
                                    itemName = itemName.substr(0,itemName.length - 1);
                                    itemId = itemId.substr(0,itemId.length - 1);
                                };
                            }else{
                                itemName='';
                                itemId='';
                            }
                            theData.html(itemName);
                            hiddenData.html(itemId);
                            parent.layer.close(index);
                        },
                        btn3:function(index, layero){
                            userSelectorWin.cancelData(index);
                            return false //开启该代码可禁止点击该按钮关闭
                        }
                    });
                    break;
                case 'role':
                    parent.layer.open({
                        type: 2
                        , title: '角色选择器'
                        , area: ['980px', '630px']
                        ,btn:["保存","取消","撤销"]
                        , resize: false
                        , content:serverPath.systemPath + setter.sysLinkPath +'dialog/view/sysRoleDialog'
                        ,success: function(layero, index){
                            body = parent.layer.getChildFrame('body',index);
                            userSelectorWin = parent.window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                            //向用户选择器页面传递初始值[{name:"name1",userId:"userId1"},{name:"name2",userId:"userId2"}...]
                            // userSelectorWin.layui.userList.push({name:"wc",userId:"ec17d71488dd43f3ac8ea1a01971d7cc"});
                            if (theData.text()) {
                                userSelectorWin.layui.roleList = [];
                                var hiddenDataArr = hiddenData.text().split(",");
                                var theDataArr = theData.text().split(",");
                                $.each(hiddenDataArr,function (k,v) {
                                    var userListObj={};
                                    userListObj.name = theDataArr[k];
                                    userListObj.roleId = v;
                                    userSelectorWin.layui.roleList.push(userListObj);
                                })
                            }
                        }
                        ,yes:function(index,layero){
                            var roleList = body.find("input[name=roleList]").val();
                            if (roleList){
                                $.each(JSON.parse(roleList),function (index, item) {
                                    itemName += item.name + ",";
                                    itemId += item.roleId + ",";
                                });
                                if (itemName.length > 0) {
                                    itemName = itemName.substr(0,itemName.length - 1);
                                    itemId = itemId.substr(0,itemId.length - 1);
                                };
                            }else{
                                itemName='';
                                itemId='';
                            }
                            theData.html(itemName);
                            hiddenData.html(itemId);
                            parent.layer.close(index);
                        },
                        btn3:function(index, layero){
                            userSelectorWin.cancelData(index);
                            return false //开启该代码可禁止点击该按钮关闭
                        }
                    });
                    break;
                case 'org':
                    parent.layer.open({
                        type: 2
                        , title: '组织选择器'
                        , area: ['980px', '630px']
                        ,btn:["保存","取消","撤销"]
                        , resize: false
                        , content: serverPath.systemPath + setter.sysLinkPath+ 'dialog/view/sysOrgDialog'
                        ,success: function(layero, index){
                            body = parent.layer.getChildFrame('body',index);
                            userSelectorWin = parent.window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                            //向用户选择器页面传递初始值[{name:"name1",userId:"userId1"},{name:"name2",userId:"userId2"}...]
                            if (theData.text()) {
                                userSelectorWin.layui.orgList = [];
                                var hiddenDataArr = hiddenData.text().split(",");
                                var theDataArr = theData.text().split(",");
                                $.each(hiddenDataArr,function (k,v) {
                                    var userListObj={};
                                    userListObj.name = theDataArr[k];
                                    userListObj.orgId = v;
                                    userSelectorWin.layui.orgList.push(userListObj);
                                })
                            }
                        }
                        ,yes: function(index, layero){
                            var theUserList = body.find("input[name=orgList]").val();
                            if (theUserList){
                                $.each(JSON.parse(theUserList),function (index, item) {
                                    itemName += item.name + ",";
                                    itemId += item.orgId + ",";
                                });
                                if (itemName.length > 0) {
                                    itemName = itemName.substr(0,itemName.length - 1);
                                    itemId = itemId.substr(0,itemId.length - 1);
                                };
                            }
                            theData.html(itemName);
                            hiddenData.html(itemId);
                            parent.layer.close(index);
                        },
                        btn3:function(index, layero){
                            userSelectorWin.cancelData(index);
                            return false //开启该代码可禁止点击该按钮关闭
                        }
                    });
                    break;
            };
        });
    });
</script>
</body>
</html>